package com.easepal802s.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.clj.fastble.data.BleDevice;
import com.easepal802s.project.utils.ActivityUtils;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.ble.HexUtils;
import com.easepal802s.project.observer.BleDataObserver;
import com.ogawa.base.utils.CommonUtil;

import java.lang.ref.WeakReference;
import java.util.Arrays;

/**
 * Created by FHB on 2018/4/9.
 */
public class LayoutFunctionView extends LinearLayout implements BleDataObserver.OnBleListerner,View.OnClickListener {
    private Context mContext;
    private ImageView mIvAdjust;
    private ImageView mIvFoodRoll;
    private ImageView mIvMusic;
    private ImageView mIvHoot;
    private ImageView mIvLed;
    private LinearLayout mLlAdjust;
    private LinearLayout mLlFoodRoll;
    private LinearLayout mLlMusic;
    private LinearLayout mLlHoot;
    private LinearLayout mLlLed;
    private MyHandler myHandler;
    private byte mByteFunction;

    public LayoutFunctionView(Context context) {
        super(context);
        mContext = context;
    }

    public LayoutFunctionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        BleDataObserver.getInst().registerBleDataObserver(this);
        myHandler = new MyHandler(this);
        mIvAdjust = findViewById(R.id.layout_function_tv_zttj);
        mIvFoodRoll = findViewById(R.id.layout_function_tv_jdgl);
        mIvMusic = findViewById(R.id.layout_function_tv_music);
        mIvHoot = findViewById(R.id.layout_function_tv_ybwg);
        mIvLed = findViewById(R.id.layout_function_tv_led);

        mIvAdjust.setOnClickListener(this);
        mIvFoodRoll.setOnClickListener(this);
        mIvMusic.setOnClickListener(this);
        mIvHoot.setOnClickListener(this);
        mIvLed.setOnClickListener(this);
    }
    public void onDestory(){
        BleDataObserver.getInst().removeBleDataObserver(this);
    }

    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
        myHandler.obtainMessage(100,data).sendToTarget();
    }
    private void getData(String data){
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            Log.e("Function", Arrays.toString(buffer));
            byte byte8 = buffer[8];
            //当数据有变化时再去判断哪个模式在运行
            if(mByteFunction!=byte8){
                mByteFunction = byte8;
                //byte8  bytePosition就 = 7
                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data,8,0))) {

                    mIvHoot.setSelected(true);
                }
                else {
                    mIvHoot.setSelected(false);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,8,1))){
                    mIvFoodRoll.setSelected(true);
                }
                else {
                    mIvFoodRoll.setSelected(false);
                }
                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data, 8, 2))) {
                    mIvLed.setSelected(true);
                }
                else {
                    mIvLed.setSelected(false);
                }
                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data, 3, 3))) {
                    mIvMusic.setSelected(true);
                }
                else {
                    mIvMusic.setSelected(false);
                }

            }
        }
    }
    @Override
    public void onPowerOff() {

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air, String name, String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {

    }

    private  class MyHandler extends Handler {
        WeakReference<LayoutFunctionView> mLayoutFunctionViewWeakReference;

        MyHandler(LayoutFunctionView mLayoutFunctionView) {
            mLayoutFunctionViewWeakReference = new WeakReference<>(mLayoutFunctionView);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutFunctionView mLayoutFunctionView = mLayoutFunctionViewWeakReference.get();
            if (mLayoutFunctionView != null) {
                String data  = (String) msg.obj;
                getData(data);


            }
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_function_tv_zttj:
                ActivityUtils.startAttitudeAdjustmentActivity(mContext);
                break;
            case R.id.layout_function_tv_jdgl:
                if(CommonUtil.isFastClick()){
                    return;
                }
                else {
                    BleController.getInst().sendSingleData(BleConstant.JIAODGL,true);
                }


                break;
            case R.id.layout_function_tv_music:
                if(CommonUtil.isFastClick()){
                    return;
                }
                else {
                   ActivityUtils.startMusicActivity(mContext);
                }
                break;
            case R.id.layout_function_tv_ybwg:
                if(CommonUtil.isFastClick()){
                    return;
                }
                else {
                    BleController.getInst().sendSingleData(BleConstant.WENGRF,true);
                }

                break;
            case R.id.layout_function_tv_led:
                if(CommonUtil.isFastClick()){
                    return;
                }
                else {
                    BleController.getInst().sendSingleData(BleConstant.LED,true);
                }

                break;


        }

    }
}
